*Listing of Abnormal and CS onstudy vital signs values;
/*data test;*/
/*	set adam.advs;*/
/*	if index(vsclsig,"OUT-OF-RANGE") and index(avisit,"Scr")=0;*/
/*run;*/

%macro print_listings(bbb, program, idisname, disname, ds_list, ttt, fr, ftnt);

proc sql;
 select count(distinct usubjid)
 into :doit
 from adamw.advs
 where	index(VSCLSIG,&bbb)>0 and saffl="Y";
 quit;


/*%if &doit>0 %then %do;*/

data vs(keep=subjid usubjid param paramcd avisit avisitn visityp ady avalc vsdt vstm saf_rev d datetm timespos);
length datetm $20. saf_rev $20.;
	set adamw.advs;
where avisitn ^in (-2,-1);
if	index(VSCLSIG,&bbb)>0 and index(avisit,"Scr")=0 ;
vsdt=put(datepart(adtm),yymmdd10.);
if atm ne . then do;
	vstm=strip(put(atm,time5.));
	if length(vstm)=4 or strip(substr(vstm,2,1))=":" then vstm="0"||strip(vstm);
		else vstm=vstm;
end;

datetm=strip(vsdt)||"/"||strip(vstm);
saf_rev=strip(substr(VSCLSIG,23));

if paramcd="DIABP" then d=1; else if paramcd="SYSBP" then d=2; else if paramcd="PULSE" then d=3; else if paramcd="RESP" then d=4;
else if paramcd="TEMP" then d=5; 
run;

proc sort data=vs;
 by d usubjid avisitn vsdt vstm;
run;

proc sql;
 select count(distinct d) into :dp from vs;
quit;

data vs1;
set vs;
by d usubjid avisitn vsdt vstm;
run;

%page_cut(in=vs1,sort=d param usubjid avisitn vsdt vstm,nocut=avisitn,newpage=,pagesize=35,skip=subjid,order=,
	columns=, width=,defln=1,nlsym=\par,out=vs2);

proc sort data=vs2(keep=page d paramcd param subjid usubjid avisitn avisit  visityp datetm vsdt vstm timespos avalc saf_rev) out=vs3;
by page d param  ; 
run;

%empty_ds_fix(vs3);

filename filetmp temp;
title3  " ";
title4 "Table &disname (Continued)";
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..VS.ABN.&fr.rtf";

%footer1(foot1=1, ds_list=&ds_list.,program=&program.);
%open_rtf;
ods listing close;
ods escapechar="^";
ods rtf file=filetmp style=styles.Algo1;

title5 "Listing of &ttt On-Study Vital Signs Values";
title6 "(Safety Population)";

/*footnote3 j=l "NCS: Not Clinically Significant / CS: Clinically Significant";*/
footnote3 j=l "[1] &ftnt";

proc report data=vs3 missing nowindows split='~' headline headskip /*spacing=1*/ nowd;
	column page d param usubjid subjid vsdt avisitn avisit datetm /*timespos*/ avalc saf_rev;

define page     / " " order order=internal noprint;
define d        / " " order order=internal noprint;

   define param    / "Parameter (Units)~ " order  style(column)=[cellwidth=2.8in just=left] style(header)=[just=left];
/*   define range    / "Regerence~Range"  order order=internal style(column)=[cellwidth=0.8in just=left] style(header)=[just=left];*/
define usubjid    / order order=internal noprint;
define subjid   / "Subject ID~ " order order=internal style(column)=[cellwidth=1.4in just=left] style(header)=[just=left];
   define vsdt    / order order=internal noprint;
   define avisitn    / order order=internal noprint;
   define avisit   / "Visit~ " order order=internal style(column)=[cellwidth=1.4in just=left] style(header)=[just=left];
   *define atptn    / order order=internal noprint;
   *define atpt   / "Time Point" order order=internal style(column)=[cellwidth=1.1in just=left] style(header)=[just=left];

/*   define visityp  / "Visit~Type" order order=internal style(column)=[cellwidth=0.9in just=left] style(header)=[just=left];*/
   *define vspos    / "Position" order order=internal style(column)=[cellwidth=1.0in just=left] style(header)=[just=left];
   define datetm     / "Date/Time~ " order order=internal style(column)=[cellwidth=1.6in just=left] style(header)=[just=left];
   *define timespos     / "Time" order order=internal style(column)=[cellwidth=1.0in just=left] style(header)=[just=left];
   define avalc    / "Value~ " order order=internal style(column)=[cellwidth=1.2in just=left] style(header)=[just=left];
   define saf_rev  / "MD Safety Review [1]~ "  order=internal style(column)=[cellwidth=1.4in just=left] style(header)=[just=left];

   break after page/page;
 
   compute before d;
    line " ";
   endcomp;

   compute after usubjid;
    line " ";
   endcomp;

 %if &doit EQ 0 %then %do;
   compute after page;
   line "No &ttt On-Study Vital Signs Values Were Measured.";
   endcomp;
 %end;
  run;
%close_rtf;
  ods rtf close;
  ods listing;
%arrange_rtf;

%mend print_listings;

%print_listings(bbb=%str("OUT-OF-RANGE"),program=T14_03_05_0X_ABN_VS, idisname=14.3.5.2, disname=14.3.5.2, ds_list=ADVS, ttt=Abnormal, fr=ALL., ftnt=%str(NCS: Not Clinically Significant / CS: Clinically Significant));

proc datasets lib=work kill nolist nodetails; run;

%print_listings(bbb=%str("OUT-OF-RANGE CS"),program=T14_03_05_0X_ABN_VS, idisname=14.3.5.3, disname=14.3.5.3, ds_list=ADVS, ttt=Clinically Significant, fr=CS., ftnt=%str(CS: Clinically Significant));

proc datasets lib=work kill nolist nodetails; run;
